import 'package:flutter/material.dart';
import 'package:aweb5/base/BaseWidget.dart';
import 'package:aweb5/pages/HomeTab.dart';
import 'package:aweb5/util/WindowUtil.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:aweb5/pages/CateTab.dart';
import 'package:aweb5/pages/Rank.dart';
//import 'package:aweb/pages/ShoppingCart.dart';
import 'package:aweb5/pages/ShoppingCart1.dart';
import 'package:badges/badges.dart';
import 'package:aweb5/pages/Member.dart';
//import 'package:amap_base_location/amap_base_location.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'HomeTabWeb.dart';
import 'package:flutter/services.dart';
import 'dart:io';
//import 'package:flutter_statusbar/flutter_statusbar.dart';
//import 'package:flutter_statusbarcolor/flutter_statusbarcolor.dart';

class HomePage4 extends BaseWidget {
  @override
  State<HomePage4> createState() => HomePage4State();
}

class HomePage4State extends BaseWidgetState<HomePage4> {
  int currentTab = 0;
  int shoppongNum = 0;
  DateTime lastPopTime;

//  List<BottomNavigationBarItem> items = [
//    BottomNavigationBarItem(icon: getIcon(0), title: getTitle(0)),
//    BottomNavigationBarItem(icon: getIcon(1), title: getTitle(1)),
//    BottomNavigationBarItem(icon: getIcon(2), title: getTitle(2)),
//    BottomNavigationBarItem(icon: getIcon(3), title: getTitle(3)),
//  ];

  @override
  void initState() {
    super.initState();
//    _initLocation();
   // WindowUtil.setStatusBar();
  }

  @override
  Widget build(BuildContext context) {
    WindowUtil.window_width = MediaQuery.of(context).size.width;
    WindowUtil.statusbar_height = MediaQuery.of(context).padding.top;
    return buildBody();
    ;
  }

  Widget buildBody(){
    if(Platform.isAndroid){
     return WillPopScope(

              child: Scaffold(

                body: getPage(),
                bottomNavigationBar:


                BottomNavigationBar(
                    selectedFontSize: 9,
                    unselectedFontSize: 9,
                    items: [
                      BottomNavigationBarItem(icon: getIcon(0), title: getTitle(0)),
                      BottomNavigationBarItem(icon: getIcon(1), title: getTitle(1)),
                      BottomNavigationBarItem(icon: getIcon(2), title: getTitle(2)),
                      BottomNavigationBarItem(icon: getIcon(3), title: getTitle(3)),
                    ],
                    onTap: tap,
                    iconSize: 10.0,
                    type: BottomNavigationBarType.fixed),
              ) ,
          onWillPop: () {
            if (lastPopTime == null ||
                DateTime.now().difference(lastPopTime) > Duration(seconds: 2)) {
              lastPopTime = DateTime.now();
              Fluttertoast.showToast(msg: "再按一次退出");
            } else {
              lastPopTime = DateTime.now();
              // 退出app
              SystemChannels.platform.invokeMethod('SystemNavigator.pop');
            }
          });
    }else if(Platform.isIOS){

           return
              Scaffold(
                body: getPage(),
                bottomNavigationBar:
                  bottom()
//                BottomNavigationBar(
//                    selectedFontSize: 9,
//                    unselectedFontSize: 9,
//                    items: [
//                      BottomNavigationBarItem(icon: getIcon(0), title: getTitle(0)),
//                      BottomNavigationBarItem(icon: getIcon(1), title: getTitle(1)),
//                      BottomNavigationBarItem(icon: getIcon(2), title: getTitle(2)),
//                      BottomNavigationBarItem(icon: getIcon(3), title: getTitle(3)),
//                    ],
//                    onTap: tap,
//                    iconSize: 10.0,
//                    type: BottomNavigationBarType.fixed),
              );

    }
  }

  Widget getTitle(int index) {
    if (index == 0) {
      if (currentTab == index) {
        return Text("首页",
            style: TextStyle(color: Color(0xffff8900), fontSize: 9));
      } else {
        return Text("首页",
            style: TextStyle(color: Color(0xffa3a3a3), fontSize: 9));
      }
    }
    if (index == 1) {
      if (currentTab == index) {
        return Text("分类",
            style: TextStyle(color: Color(0xffff8900), fontSize: 9));
      } else {
        return Text("分类",
            style: TextStyle(color: Color(0xffa3a3a3), fontSize: 9));
      }
    }
    if (index == 2) {
      if (currentTab == index) {
        return Text("购物车",
            style: TextStyle(color: Color(0xffff8900), fontSize: 9));
      } else {
        return Text("购物车",
            style: TextStyle(color: Color(0xffa3a3a3), fontSize: 9));
      }
    }
    if (index == 3) {
      if (currentTab == index) {
        return Text("我的",
            style: TextStyle(color: Color(0xffff8900), fontSize: 9));
      } else {
        return Text("我的",
            style: TextStyle(color: Color(0xffa3a3a3), fontSize: 9));
      }
    }
  }

  Widget getIcon(int index) {
    if (index == 0) {
      if (currentTab == index) {
        return Container(
          width: 25,
          height: 25,
          alignment: Alignment.bottomCenter,
          child: Image.asset(
            "assets/homeselect.png",
            width: 20.0,
            height: 20,
            fit: BoxFit.fill,
          ),
        );
      } else {
        return Container(
          width: 25,
          height: 25,
          alignment: Alignment.bottomCenter,
          child: Image.asset(
            "assets/home.png",
            width: 20.0,
            height: 20,
            fit: BoxFit.fill,
          ),
        );
      }
    }
    if (index == 1) {
      if (currentTab == index) {
        return Container(
          width: 25,
          height: 25,
          alignment: Alignment.bottomCenter,
          child: Image.asset(
            "assets/chanpinselect.png",
            width: 20.0,
            height: 20,
            fit: BoxFit.fill,
          ),
        );
      } else {
        return Container(
          width: 25,
          height: 25,
          alignment: Alignment.bottomCenter,
          child: Image.asset(
            "assets/chanpin.png",
            width: 20.0,
            height: 20,
            fit: BoxFit.fill,
          ),
        );
      }
    }
    if (index == 2) {
      if (currentTab == index) {
        return Container(
          width: 25,
          height: 25,
          child: Stack(
            children: <Widget>[
              Container(
                width: 25,
                height: 25,
                alignment: Alignment.bottomCenter,
                child: Image.asset(
                  "assets/gouwucheselect.png",
                  width: 20.0,
                  height: 20.0,
                  fit: BoxFit.fill,
                ),
              ),
              Positioned(
                child: customBage(),
                right: 0,
                top: 0,
              )
            ],
          ),
        );
      } else {
        return Container(
          width: 25,
          height: 25,
          child: Stack(
            children: <Widget>[
              Container(
                width: 25,
                height: 25,
                alignment: Alignment.bottomCenter,
                child: Image.asset(
                  "assets/gouwuche.png",
                  width: 20.0,
                  height: 20.0,
                  fit: BoxFit.fill,
                ),
              ),
              Positioned(
                child: customBage(),
                right: 0,
                top: 0,
              )
            ],
          ),
        );
      }
    }
//    assets/wode_morenselect.png
//    assets/wode_moren.png
    if (index == 3) {
      if (currentTab == index) {
        return Container(
          width: 25,
          height: 25,
          alignment: Alignment.bottomCenter,
          child: Image.asset(
            "assets/wode_morenselect.png",
            width: 20.0,
            height: 20,
            fit: BoxFit.fill,
          ),
        );
      } else {
        return Container(
          width: 25,
          height: 25,
          alignment: Alignment.bottomCenter,
          child: Image.asset(
            "assets/wode_moren.png",
            width: 20.0,
            height: 20,
            fit: BoxFit.fill,
          ),
        );
      }
    }
  }

  void tap(int index) {
    // toast("${index}");
    currentTab = index;
    setState(() {});
  }

  Widget getPage() {
    if (currentTab == 0) {
      return
        SafeArea(child: HomeTabWeb())        ;
    }
    if (currentTab == 1) {
      return
        SafeArea(child: CateTab());

    }
    if (currentTab == 2) {
      return
        SafeArea(child: ShoppingCart(callback))
        ;
    }
    if (currentTab == 3) {
      return
        SafeArea(child: Member())
        ;
    }
  }

  callback(shopNum) {
    shoppongNum = shopNum;
    setState(() {});
  }

  Widget bage() {
    return Opacity(
      opacity: shoppongNum == 0 ? 0.0 : 1.0,
      child: Badge(
        position: BadgePosition(top: 0, right: 0),
        // padding: EdgeInsets.all(0),
        badgeColor: Colors.red,
        shape: BadgeShape.circle,
//        borderRadius: 10,
        toAnimate: false,
        badgeContent: Text('${shoppongNum}',
            style: TextStyle(color: Colors.white, fontSize: 10)),
      ),
    );
  }

  Widget customBage() {
    return Opacity(
      opacity: shoppongNum == 0 ? 0.0 : 1.0,
      child: Container(
        decoration: BoxDecoration(color: Colors.red, shape: BoxShape.circle),
        padding: EdgeInsets.all(5),
        child: Text('${shoppongNum}',
            style: TextStyle(color: Colors.white, fontSize: 10)),
      ),
    );
  }

//  final _amapLocation = AMapLocation();
//  var _result = '';
//
//  //初始化定位监听
//  void _initLocation() async {
//    _amapLocation.init();
//
//    final options = LocationClientOptions(
//      isOnceLocation: false,
//      locatingWithReGeocode: true,
//    );
//
//    if (await Permissions.requestMapPermission()) {
//      _amapLocation.startLocate(options).listen((_) => setState(() {
//            _result =
//                '坐标：${_.longitude}，${_.latitude} @ ${DateTime.now().hour}:${DateTime.now().minute}:${DateTime.now().second}';
//            // print(_.latitude);
//            // Fluttertoast.showToast(msg: "${_.latitude}");
//          }));
//    } else {
//      setState(() {
//        _result = "无定位权限";
//      });
//    }
//  }
//  Future initStatus() async{
//    WindowUtil.statusbar_height=await FlutterStatusbar.height;
//  }

  Widget bottom(){
    BottomAppBar(

      color: Colors.red,
      shape: CircularNotchedRectangle(),
      child: Padding(
        padding: EdgeInsets.fromLTRB(0, 6, 0, 6),
        child: Row(
          mainAxisSize: MainAxisSize.max,
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: <Widget>[
            GestureDetector(
                onTap: () {
                  onTap(0);
                },
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: <Widget>[
                    getIcon1(0),
                    getTitle1(0)
                  ],
                )),
            GestureDetector(
                onTap: () {
                  onTap(1);
                },
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: <Widget>[
                    getIcon1(1),
                    getTitle1(1)
                  ],
                )),
            Column(
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                getIcon1(2),
                getTitle1(2)
              ],
            ),
            GestureDetector(
                onTap: () {
                  onTap(2);
                },
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: <Widget>[
                    getIcon1(3),
                    getTitle1(3)
                  ],
                )),
            GestureDetector(
                onTap: () {
                  onTap(3);
                },
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: <Widget>[
                    getIcon1(4),
                    getTitle1(4)
                  ],
                ))
          ],
        ),
      ),
    );
  }
  void onTap(int index) {
    // toast("${index}");
    currentTab = index;
    setState(() {});
  }

  Widget getTitle1(int index) {
    if (index == 0) {
      if (currentTab == index) {
        return Text("首页",
            style: TextStyle(color: Color(0xffff8900), fontSize: 9));
      } else {
        return Text("首页",
            style: TextStyle(color: Color(0xffa3a3a3), fontSize: 9));
      }
    }
    if (index == 1) {
      if (currentTab == index) {
        return Text("分类",
            style: TextStyle(color: Color(0xffff8900), fontSize: 9));
      } else {
        return Text("分类",
            style: TextStyle(color: Color(0xffa3a3a3), fontSize: 9));
      }
    }
    if (index == 2) {
      if (currentTab == index) {
        return Text("购物车",
            style: TextStyle(color: Color(0xffff8900), fontSize: 9));
      } else {
        return Text("购物车",
            style: TextStyle(color: Color(0xffa3a3a3), fontSize: 9));
      }
    }
    if (index == 3) {
      if (currentTab == index) {
        return Text("购物车",
            style: TextStyle(color: Color(0xffff8900), fontSize: 9));
      } else {
        return Text("购物车",
            style: TextStyle(color: Color(0xffa3a3a3), fontSize: 9));
      }
    }
    if (index == 3) {
      if (currentTab == index) {
        return Text("我的",
            style: TextStyle(color: Color(0xffff8900), fontSize: 9));
      } else {
        return Text("我的",
            style: TextStyle(color: Color(0xffa3a3a3), fontSize: 9));
      }
    }
  }



  Widget getIcon1(int index) {
    if (index == 0) {
      if (currentTab == index) {
        return Container(
          width: 25,
          height: 25,
          alignment: Alignment.bottomCenter,
          child: Image.asset(
            "assets/homeselect.png",
            width: 20.0,
            height: 20,
            fit: BoxFit.fill,
          ),
        );
      } else {
        return Container(
          width: 25,
          height: 25,
          alignment: Alignment.bottomCenter,
          child: Image.asset(
            "assets/home.png",
            width: 20.0,
            height: 20,
            fit: BoxFit.fill,
          ),
        );
      }
    }
    if (index == 1) {
      if (currentTab == index) {
        return Container(
          width: 25,
          height: 25,
          alignment: Alignment.bottomCenter,
          child: Image.asset(
            "assets/chanpinselect.png",
            width: 20.0,
            height: 20,
            fit: BoxFit.fill,
          ),
        );
      } else {
        return Container(
          width: 25,
          height: 25,
          alignment: Alignment.bottomCenter,
          child: Image.asset(
            "assets/chanpin.png",
            width: 20.0,
            height: 20,
            fit: BoxFit.fill,
          ),
        );
      }
    }
    if (index == 2) {
      if (currentTab == index) {
        return Container(
          width: 25,
          height: 25,
          child: Stack(
            children: <Widget>[
              Container(
                width: 25,
                height: 25,
                alignment: Alignment.bottomCenter,
                child: Image.asset(
                  "assets/gouwucheselect.png",
                  width: 20.0,
                  height: 20.0,
                  fit: BoxFit.fill,
                ),
              ),
              Positioned(
                child: customBage(),
                right: 0,
                top: 0,
              )
            ],
          ),
        );
      } else {
        return Container(
          width: 25,
          height: 25,
          child: Stack(
            children: <Widget>[
              Container(
                width: 25,
                height: 25,
                alignment: Alignment.bottomCenter,
                child: Image.asset(
                  "assets/gouwuche.png",
                  width: 20.0,
                  height: 20.0,
                  fit: BoxFit.fill,
                ),
              ),
              Positioned(
                child: customBage(),
                right: 0,
                top: 0,
              )
            ],
          ),
        );
      }
    }
//    assets/wode_morenselect.png
//    assets/wode_moren.png
    if (index == 3) {
      if (currentTab == index) {
        return Container(
          width: 25,
          height: 25,
          alignment: Alignment.bottomCenter,
          child: Image.asset(
            "assets/wode_morenselect.png",
            width: 20.0,
            height: 20,
            fit: BoxFit.fill,
          ),
        );
      } else {
        return Container(
          width: 25,
          height: 25,
          alignment: Alignment.bottomCenter,
          child: Image.asset(
            "assets/wode_moren.png",
            width: 20.0,
            height: 20,
            fit: BoxFit.fill,
          ),
        );
      }
    }
  }
}
